package edu.spec.pms.dao.impl;

import org.madgecko.basic.dao.BaseDao;
import edu.spec.pms.dao.iface.ICommandDao;
import edu.spec.pms.model.sys.Command;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class CommandDao extends BaseDao<Command> implements ICommandDao{

    private String generateNewStrFromCommand(){
        return "select new Command(c.id, c.chinese, c.english, c.url, c.iconClz, c.orderNum, c.module.id) ";
    }

    private String generateNewStrFromRoleCommand(){
		return "select new Command(rc.command.id, rc.command.chinese, rc.command.english, rc.command.url, rc.command.iconClz, rc.command.orderNum, rc.command.module.id) ";
    }

    private String generateDistinctNewStrFromRoleCommand(){
        return "select distinct new Command(rc.command.id, rc.command.chinese, rc.command.english, rc.command.url, rc.command.iconClz, rc.command.orderNum, rc.command.module.id) ";
    }

	@Override
	public void deleteCommandByModule(int moduleId) {
		String hql = "delete Command c where c.module.id=?";
		this.updateByHql(hql, moduleId);
	}

	@Override
    public List<Command> listAllCommand() {
        //1条sql
        String hql = generateNewStrFromCommand() + " from Command c";
        List<Command> lcs = this.list(hql);
        return lcs;
    }

	@Override
	public List<Command> listCommandByModule(int moduleId) {
		String hql = "select c from Command c where c.module.id=? order by c.orderNum";
		return this.list(hql, moduleId);
	}

	@Override
    public List<Command> listCommandByRoles(List<Integer> roleIds) {
        // 1条sql
        // distinct在new前同样适用
		if(roleIds.size()==0){
			return new ArrayList<Command>();
		}
        String ids = "(";
        for(Integer id : roleIds){
            ids += id + ", ";
        }
		ids = ids.substring(0, ids.length()-2);
        ids += ")";
        String hql = generateDistinctNewStrFromRoleCommand() + " from RoleCommand rc where rc.role.status=1 and rc.command.status=1 and rc.role.id in " + ids ;
        return this.list(hql);
    }

    @Override
    public List<Command> listCommandByRole(Integer roleId) {
        //1条sql
        String hql = generateNewStrFromRoleCommand() + " from RoleCommand rc where rc.role.id=?";
        return this.list(hql, roleId);
    }

    @Override
    public List<Command> listCommandByUser(Integer userId) {
        //command里需带上moduleId,界面通过moduleId来找command
        //2种方案。一是使用sql，但需新建一个不级联module的类型。二是使用hql，但command会重复(行不通)。
        //
//        String sql = "select distinct c.id as id c.chinese as chinese  from " +
//                "t_role_command rc left join t_command c on(rc.command_id=c.id) " +
//                "left join t_user_role ur on(rc.role_id=ur.role_id) where ur.user_id=?";
        String hql = "select distinct new Command(c.id, c.chinese, c.english, c.url, c.iconClz, c.orderNum, c.module.id) from " +
                "RoleCommand rc left join fetch rc.command c " +
                "left join fetch UserRole ur on(rc.command.id=ur.role.id) where ur.user.id=?";
        return this.list(hql, userId);
    }
}
